home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 8
/
Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso
/
Aminet
/
util
/
cdity
/
Yak210src.lha
/
Yak_2.10_Src
/
Prefs
/
main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-08-13
|
24KB
|
975 lines
/*
* Yak version 2.10
* ----------------
* [Yak == Yet Another K(?)ommodity
*
* There seems to be a profusion of commodities doing this or that.
* Heres mine, to do what I want it to:
*
* AutoActivate windows (SunMouse)
* ClickToFront, ClickToBack, ScreenCycle
* Close/Zip/Shrink/Zoom/Turn a window via keyboard.
* Bring up a palette on front screen.
* Insert date into read-stream.
* Produce key-click (like my keyclick program).
* Some other things...
*
* Martin W. Scott, Gaël Marziou & Philippe Bastiani, 7/94.
*/
#define __USE_SYSBASE
#include <exec/types.h>
#include <exec/libraries.h>
#include <exec/memory.h>
#include <devices/inputevent.h>
#include <diskfont/diskfont.h>
#include <dos/dos.h>
#include <dos/dostags.h>
#include <dos/notify.h>
#include <graphics/displayinfo.h>
#include <libraries/commodities.h>
#include <libraries/reqtools.h>
#include <libraries/locale.h>
#include <intuition/intuitionbase.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <clib/alib_protos.h>
/* MUI */
#ifdef MUI /* NMC */
#include <libraries/mui.h>
#include <proto/muimaster.h>
#endif /* NMC */
/* BGUI */
#ifdef BGUI /* NMC */
#include <libraries/bgui.h>
#include <libraries/bgui_macros.h>
#include <proto/bgui.h>
#endif
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/graphics.h>
#include <proto/commodities.h>
#include <proto/diskfont.h>
#include <proto/icon.h>
#include <proto/iffparse.h>
#include <proto/intuition.h>
#include <proto/keymap.h>
#include <proto/layers.h>
#include <proto/locale.h>
#include <proto/reqtools.h>
#include <proto/wb.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "code.h"
#include "yak.h"
#include "Handlers.h"
#include "hotkey_types.h"
#include "icon.h"
#include "gui.h"
#include "Settings.h"
#include "Requesters.h"
#include "Version.h"
#include "yak_locale_strings.h"
/* local prototypes for main.c */
static void CloseResources(void);
static BOOL OpenResources(void);
static void FreePatterns(void);
static LONG ProcessMsg(void);
static void NotifyNewPrefs(void);
static SAVEDS LONG ASM HandleAppMsg(REG(a0) struct Hook *hook,REG(a2) APTR obj,REG(a1) struct AppMessage **amsg);
void STACKARGS MAIN(char *);
LONG (*HandleIDCMP)(void); /* GadTools */
LONG (*HandleReturnID)(LONG ID); /* MUI & BGUI */
extern struct WBStartup *WBMsg;
/*
* libraries opened by startup code; basepointers needed by function pragmas
*/
#ifdef _DCC
extern struct ExecBase *SysBase;
extern struct DosLibrary *DOSBase;
struct Library *CxBase, *DiskfontBase,
*IFFParseBase, *KeymapBase;
#endif
BOOL CreateIcons;
char *ProgramName=NULL;
char *PrefsFile=NULL;
IMPORT char FontName[];
IMPORT int FontSize;
IMPORT struct TextFont *TextFont;
IMPORT struct TextAttr Attr; /*NMC*/
BYTE broker_pri;
struct MsgPort *broker_mp=NULL;
CxObj *broker=NULL;
extern struct MsgPort *AppMsgPort;
extern ULONG appsigflag;
ULONG wndsigflag; /* here for overlay purposes */
ULONG cxsigflag=0L;
ULONG invariantsigflag;
BOOL NewPrefs=FALSE;
static LONG notifysigbit;
static ULONG notifysigflag=0L;
struct NotifyRequest NotifyRequest;
BOOL YakIconified=FALSE;
#if defined(GADTOOLS) || defined(BGUI)
/*
* Determine GUI font according to FONT tooltype NMC
* Used by both GADTOOLS and BGUI.
*/
VOID
ReadFont( char *Name, int *Size)
{
if (sscanf(TTString("FONT", ""),"%s %d", Name, Size) >= 2)
{
if (Name[0] == '"')
{
sscanf(TTString("FONT", ""),"\" %s %d", Name, Size);
}
strcat(Name, ".font");
}
else
{
Name[0] = '\0';
*Size = 8;
}
}
#endif
#ifdef MUI
/*
*
* MUI Specific Code
*
*/
struct Library *MUIMasterBase;
APTR App=NULL;
struct Hook AppMsgHook = { { NULL,NULL },(void *)HandleAppMsg,NULL,NULL };
/* close what we opened */
static void
CloseResources()
{
/* NULL pointers are valid so don't waste time to test them */
CloseLibrary(MUIMasterBase);
CloseLibrary(CxBase);
CloseLibrary(DiskfontBase);
CloseLibrary(IFFParseBase);
CloseLibrary(KeymapBase);
CloseLocaleStuff();
}
/* open libraries, devices that we need */
static BOOL
OpenResources(void)
{
if ((MUIMasterBase = OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN)) &&
(CxBase = OpenLibrary("commodities.library", 37L)) &&
(DiskfontBase = OpenLibrary("diskfont.library", 36L)) &&
(IFFParseBase = OpenLibrary("iffparse.library", 37L)) &&
(KeymapBase = OpenLibrary("keymap.library", 37L)))
{
return TRUE;
}
CloseResources();
return FALSE;
}
BOOL
CreateApp(void)
{
/* Set up the MUI application */
App = ApplicationObject,
MUIA_Application_Author, AUTHOR,
MUIA_Application_Title, "Yak_Prefs",
MUIA_Application_Version, versionstr,
MUIA_Application_Copyright, COPYRIGHT_STR,
MUIA_Application_Description, "Yak Preferences",
MUIA_Application_BrokerPri, (LONG)broker_pri,
MUIA_Application_SingleTask, TRUE,
MUIA_Application_Active, TRUE,
End;
if (App)
{
DoMethod(App,
MUIM_Notify, MUIA_Application_DoubleStart, TRUE,
App,
2,
MUIM_Application_ReturnID, ID_SHOW
);
DoMethod(App,
MUIM_Notify,
MUIA_Application_Iconified, TRUE,
App,
2,
MUIM_Application_ReturnID, ID_HIDE
);
DoMethod(App,
MUIM_Notify,
MUIA_Application_Iconified, FALSE,
App,
2,
MUIM_Application_ReturnID, ID_SHOW
);
get(App, MUIA_Application_BrokerPort, broker_mp);
return TRUE;
}
return FALSE;
}
void
DisposeApp(void)
{
if (App) MUI_DisposeObject(App);
}
static LONG
ProcessMsg(void)
{
BOOL running=TRUE;
ULONG signal, sigrcvd;
LONG id;
switch (id=DoMethod(App, MUIM_Application_Input, &signal))
{
case MUIV_Application_ReturnID_Quit:
running = FALSE;
break;
case ID_HIDE:
YakIconified = TRUE;
break;
case ID_SHOW:
if (YakIconified)
{
YakIconified = FALSE;
set(App, MUIA_Application_Iconified, FALSE);
}
if (NewPrefs)
{
/* Settings have changed */
if (GetOrders(getString(Update_Restore_ORDERS),
getString(Prefs_modified_STRING)))
{
LoadSettings(ENV_CONFIG_FILE);
HideInterface();
curwinID = ROOT_WINDOW;
NewPrefs = FALSE;
}
}
if (!ShowYakInterface())
{
running = FALSE;
}
break;
default:
if ((id>0) && ((*HandleReturnID)(id) != RET_OKAY))
running = FALSE;
break;
}
if (running && signal)
{
sigrcvd = Wait(signal | invariantsigflag);
if (sigrcvd & notifysigflag)
{
NotifyNewPrefs();
}
if (sigrcvd & SIGBREAKF_CTRL_C)
running = FALSE;
}
return running;
}
#endif /* MUI */
#ifdef BGUI
/*
*
* BGUI Specific Code NMC
*
*/
struct Library *BGUIBase;
Object *MyCxObj;
/* close what we opened */
static void
CloseResources()
{
/* NULL pointers are valid so don't waste time to test them */
CloseLibrary((struct Library *)IntuitionBase);
CloseLibrary((struct Library *)GfxBase);
CloseLibrary(CxBase);
CloseLibrary(DiskfontBase);
CloseLibrary(IconBase);
CloseLibrary(BGUIBase);
CloseLibrary(WorkbenchBase);
CloseLibrary(IFFParseBase);
CloseLibrary(KeymapBase);
CloseLocaleStuff();
}
/* open libraries, devices that we need */
static BOOL
OpenResources(void)
{
if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37L)) &&
(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37L)) &&
(CxBase = OpenLibrary("commodities.library", 37L)) &&
(DiskfontBase = OpenLibrary("diskfont.library", 36L)) &&
(IconBase = OpenLibrary("icon.library", 37L)) &&
(BGUIBase = OpenLibrary("bgui.library", 38L)) &&
(WorkbenchBase = OpenLibrary("workbench.library", 37L)) &&
(IFFParseBase = OpenLibrary("iffparse.library", 37L)) &&
(KeymapBase = OpenLibrary("keymap.library", 37L)))
{
return TRUE;
}
CloseResources();
return FALSE;
}
BOOL
CreateApp(void)
{
if (MyCxObj = CommodityObject,
COMM_Name, "Yak_Prefs",
COMM_Title, VERSION_BROKER,
COMM_Description, "Yak Preferences",
COMM_Priority, broker_pri,
COMM_Unique, TRUE,
COMM_Notify, TRUE,
COMM_ShowHide, TRUE,
EndObject)
{
EnableBroker(MyCxObj);
GetAttr(COMM_SigMask,MyCxObj,&cxsigflag);
/* if FONT tooltype supplied, open font */
if (*FontName)
{
Attr.ta_Name = FontName;
Attr.ta_YSize = FontSize;
Attr.ta_Style = 0;
Attr.ta_Flags = 0;
TextFont = OpenDiskFont(&Attr);
}
return(TRUE);
}
else
PostError(getString(Allocation_ERR));
return(FALSE);
}
void
DisposeApp(void)
{
DisposeObject(MyCxObj);
if (TextFont) CloseFont(TextFont);
}
/* monitor ports, act on messages */
static LONG
ProcessMsg(void)
{
ULONG sigrcvd, msgid, msgtype,msgdata;
LONG returnvalue = 1L;
if (curwin)
{
GetAttr(WINDOW_SigMask,curwinobj,&wndsigflag);
GetAttr(WINDOW_AppMask,curwinobj,&appwinsigflag);
}
else
wndsigflag = appwinsigflag = 0;
sigrcvd = Wait(invariantsigflag | wndsigflag | appwinsigflag);
if (sigrcvd & appwinsigflag)
{
struct AppMessage *amsg;
while(amsg = GetAppMsg(curwinobj))
{
HandleAppMsg(NULL,NULL,&amsg);
ReplyMsg((struct Message *)amsg);
}
}
if (sigrcvd & wndsigflag)
{
while(curwin && ((msgid = HandleEvent(curwinobj)) != WMHI_NOMORE))
{
if ((*HandleReturnID)(msgid) != RET_OKAY)
returnvalue = 0;
}
}
if (sigrcvd & notifysigflag) /* settings have changed */
{
NotifyNewPrefs();
}
while(DoMethod(MyCxObj,CM_MSGINFO,
&msgtype,&msgid,&msgdata) != CMMI_NOMORE)
{
switch(msgtype)
{
case CXM_COMMAND:
switch(msgid)
{
case CXCMD_UNIQUE:
case CXCMD_APPEAR:
YakIconified = FALSE;
if (NewPrefs)
{
/* Settings have changed */
if (GetOrders(getString(Update_Restore_ORDERS),
getString(Prefs_modified_STRING)))
{
LoadSettings(ENV_CONFIG_FILE);
curwinID = ROOT_WINDOW;
NewPrefs = FALSE;
}
}
if (!ShowYakInterface())
{
returnvalue = 0L;
}
break;
case CXCMD_DISAPPEAR:
YakIconified = TRUE;
HideInterface();
break;
case CXCMD_DISABLE:
DisableBroker(MyCxObj);
break;
case CXCMD_ENABLE:
EnableBroker(MyCxObj);
break;
case CXCMD_KILL:
returnvalue = 0L;
break;
}
break;
}
}
if (sigrcvd & SIGBREAKF_CTRL_C)
returnvalue = 0L;
return(returnvalue);
}
#endif /* BGUI */
#ifdef GADTOOLS
/*
*
* GadTools Specific Code
*
*/
#ifdef _DCC
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *IconBase, *GadToolsBase,
*WorkbenchBase;
#endif
struct NewBroker newbroker = {
NB_VERSION,
"Yak_Prefs", /* string to identify this broker */
VERSION_BROKER,
"Yak Preferences",
NBU_UNIQUE | NBU_NOTIFY, /* Don't want any new commodities
* starting with this name. If someone
* tries it, let me know */
COF_SHOW_HIDE
};
/* close what we opened */
static void
CloseResources()
{
/* NULL pointers are valid so don't waste time to test them */
CloseLibrary((struct Library *)IntuitionBase);
CloseLibrary((struct Library *)GfxBase);
CloseLibrary(CxBase);
CloseLibrary(DiskfontBase);
CloseLibrary(IconBase);
CloseLibrary(GadToolsBase);
CloseLibrary(WorkbenchBase);
CloseLibrary(IFFParseBase);
CloseLibrary(KeymapBase);
CloseLocaleStuff();
}
/* open libraries, devices that we need */
static BOOL
OpenResources(void)
{
if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37L)) &&
(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37L)) &&
(CxBase = OpenLibrary("commodities.library", 37L)) &&
(DiskfontBase = OpenLibrary("diskfont.library", 36L)) &&
(IconBase = OpenLibrary("icon.library", 37L)) &&
(GadToolsBase = OpenLibrary("gadtools.library", 37L)) &&
(WorkbenchBase = OpenLibrary("workbench.library", 37L)) &&
(IFFParseBase = OpenLibrary("iffparse.library", 37L)) &&
(KeymapBase = OpenLibrary("keymap.library", 37L)))
{
return TRUE;
}
CloseResources();
return FALSE;
}
BOOL
CreateApp(void)
{
if (broker_mp=CreateMsgPort())
{
newbroker.nb_Port = broker_mp;
cxsigflag = 1L << broker_mp->mp_SigBit;
newbroker.nb_Pri = broker_pri;
if (broker=CxBroker(&newbroker, NULL))
{
if ((AppMsgPort=CreateMsgPort()))
{
appsigflag = 1L << AppMsgPort->mp_SigBit;
return TRUE;
}
else PostError(getString(Allocation_ERR));
}
}
else PostError(getString(Allocation_ERR));
return FALSE;
}
void
DisposeApp(void)
{
if (AppMsgPort) DeleteMsgPort(AppMsgPort);
if (broker) DeleteCxObjAll(broker);
if (broker_mp) DeleteMsgPort(broker_mp);
}
/* monitor cx port, act on messages */
static LONG
ProcessMsg(void)
{
CxMsg *msg;
ULONG sigrcvd, msgid, msgtype;
LONG returnvalue = 1L;
sigrcvd = Wait(invariantsigflag | wndsigflag);
if (sigrcvd & appsigflag)
{
struct AppMessage *amsg;
while (amsg=(struct AppMessage *)GetMsg(AppMsgPort))
{
HandleAppMsg(NULL,NULL,&amsg);
ReplyMsg((struct Message *)amsg);
}
}
if (sigrcvd & wndsigflag)
if ((*HandleIDCMP)() != RET_OKAY)
returnvalue = 0;
if (sigrcvd & notifysigflag) /* settings have changed */
{
NotifyNewPrefs();
}
while(msg = (CxMsg *)GetMsg(broker_mp))
{
msgid = CxMsgID(msg);
msgtype = CxMsgType(msg);
ReplyMsg((struct Message *)msg);
switch(msgtype)
{
case CXM_COMMAND:
switch(msgid)
{
case CXCMD_UNIQUE:
case CXCMD_APPEAR:
YakIconified = FALSE;
if (NewPrefs)
{
/* Settings have changed */
if (GetOrders(getString(Update_Restore_ORDERS),
getString(Prefs_modified_STRING)))
{
LoadSettings(ENV_CONFIG_FILE);
curwinID = ROOT_WINDOW;
NewPrefs = FALSE;
}
}
if (!ShowYakInterface())
{
returnvalue = 0L;
}
break;
case CXCMD_DISAPPEAR:
YakIconified = TRUE;
HideInterface();
break;
case CXCMD_DISABLE:
ActivateCxObj(broker, 0L);
break;
case CXCMD_ENABLE:
ActivateCxObj(broker, 1L);
break;
case CXCMD_KILL:
returnvalue = 0L;
break;
}
break;
}
}
if (sigrcvd & SIGBREAKF_CTRL_C)
returnvalue = 0L;
return(returnvalue);
}
#endif /* GADTOOLS */
/*
*
* Shared Code
*
*/
static SAVEDS LONG ASM
HandleAppMsg(REG(a0) struct Hook *hook,
REG(a2) APTR obj,
REG(a1) struct AppMessage **am)
{
struct AppMessage *amsg=*am;
if (PrefsFile) FreeVec(PrefsFile);
if (PrefsFile=AllocVec(256, MEMF_CLEAR))
{
/* Get first argument */
struct WBArg *wbarg=amsg->am_ArgList;
if (NameFromLock(wbarg->wa_Lock, PrefsFile, 256))
AddPart(PrefsFile, wbarg->wa_Name, 256);
}
#ifdef MUI
if (YakIconified)
{
YakIconified = FALSE;
set(App, MUIA_Application_Iconified, FALSE);
}
#endif
CloseWindowID(curwinID);
LoadSettings(PrefsFile);
curwinID = ROOT_WINDOW;
ShowYakInterface();
return 0L;
}
static void
NotifyNewPrefs(void)
{
if (!YakIconified)
{
/* GUI is opened */
DisableYakInterface();
if (GetOrders(getString(Update_Restore_ORDERS),
getString(Prefs_modified_STRING)))
{
CloseWindowID(curwinID);
LoadSettings(ENV_CONFIG_FILE);
curwinID = ROOT_WINDOW;
NewPrefs = FALSE;
ShowYakInterface();
}
else
EnableYakInterface();
}
else
NewPrefs = TRUE; /* Yak-Prefs: HIDE mode */
}
void
EndNotification(void)
{
EndNotify(&NotifyRequest);
FreeSignal(notifysigbit);
}
void
SetNotification(void)
{
/* Allocate a signalsbit */
if ((notifysigbit = AllocSignal(-1L)) != -1)
{
notifysigflag = 1 << notifysigbit;
/* Initialize notification request */
NotifyRequest.nr_Name = ENV_CONFIG_FILE;
NotifyRequest.nr_Flags = NRF_SEND_SIGNAL;
/* Signal this task */
NotifyRequest.nr_stuff.nr_Signal.nr_Task = (struct Task *) FindTask(NULL);
/* with this signals bit */
NotifyRequest.nr_stuff.nr_Signal.nr_SignalNum = notifysigbit;
StartNotify(&NotifyRequest);
}
}
static void
FreePatterns()
{
UWORD i;
for (i = 0; i < NUM_PATTERNS; i++)
if (patterns[i].pat) FreeVec(patterns[i].pat);
}
#ifdef _DCC
void _waitwbmsg(void);
static void
uncalled(void)
{
_waitwbmsg();
}
#endif
void
#ifdef FORTIFY
main(int argc, char **argv)
#else
STACKARGS MAIN(char *arg) /* Yak: multi-function commodity */
#endif
{
#ifdef FORTIFY
Fortify_EnterScope();
Fortify_CheckAllMemory();
#endif
if (OpenResources())
{
if (WBMsg) /* WB Startup */
{
struct WBArg *wbarg=&WBMsg->sm_ArgList[WBMsg->sm_NumArgs-1];
if (WBMsg->sm_NumArgs > 1)
{
BPTR dirlock;
/* Go to icon directory */
dirlock = CurrentDir(wbarg->wa_Lock);
/* Install prefs */
CopyFile(wbarg->wa_Name, ENV_CONFIG_FILE);
/* Go back and exit... */
CurrentDir(dirlock);
FreeOurIcon();
CloseResources();
return;
}
else
{
/* Get Program Name */
if (ProgramName=AllocVec(256, MEMF_CLEAR))
{
if (NameFromLock(wbarg->wa_Lock, ProgramName, 256))
AddPart(ProgramName, wbarg->wa_Name, 256);
}
}
}
else /* CLI Startup */
{
/* Get Program Name */
if (ProgramName=AllocVec(256, MEMF_CLEAR))
{
BPTR lock;
GetProgramName(ProgramName, 256);
if (lock=Lock(ProgramName, ACCESS_READ))
{
NameFromLock(lock, ProgramName, 256);
UnLock(lock);
}
}
}
/* */
if (PrefsFile=AllocVec(256, MEMF_CLEAR))
{
BPTR lock=GetProgramDir();
if (NameFromLock(lock, PrefsFile, 256))
AddPart(PrefsFile, "Presets/", 256);
}
/* process tool-types */
GetOurIcon(ProgramName);
/* Open the right locale if tooltype LANGUAGE is used */
OpenLocaleStuff(TTString("LANGUAGE",NULL));
#if defined(GADTOOLS) || defined(BGUI)
ReadFont( FontName, &FontSize);
#endif
CreateIcons = TTBool("CREATEICONS", TRUE);
broker_pri = (BYTE)TTInt("CX_PRIORITY", 0);
FreeOurIcon();
if (CreateApp())
{
InitYakHotKeyList();
LoadSettings(ENV_CONFIG_FILE);
#ifdef GADTOOLS
ActivateCxObj(broker, 1L);
#endif
/*
* We want to be notified each time either
* ENV:yak.prefs is modified (by Yak Preferences)
*/
SetNotification();
/* these are the signals waited for, + window sig */
invariantsigflag = SIGBREAKF_CTRL_C | cxsigflag | appsigflag | notifysigflag;
if (ShowYakInterface())
{
while (ProcessMsg())
;
HideInterface();
}
DeleteYakHotKeyList();
EndNotification();
FreePatterns();
CleanMouseCycling();
}
DisposeApp();
if (ProgramName) FreeVec(ProgramName);
if (PrefsFile) FreeVec(PrefsFile);
#ifdef FORTIFY
Fortify_LeaveScope();
#endif
CloseResources();
}
else PostError(getString(Resource_ERR));
}